<template>
  <div class="mod-prod">
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :table-loading="dataListLoading"
               :option="tableOption"
               @search-change="searchChange"
               @selection-change="selectionChange"
               @on-load="getDataList"
               @refresh-change="refreshChange">
      <template slot="menuLeft">
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="small"
                   @click.stop="addOrUpdateHandle()">新增</el-button>
      </template>

      <template slot-scope="scope"
                slot="status">
        <el-tag v-if="scope.row.status == 1"
                size="small">上架</el-tag>
        <el-tag v-else
                size="small" type="danger">下架</el-tag>
      </template>

      <template slot-scope="scope"
                slot="menu">
        <el-button type="text"
                   @click="addOrUpdateHandle(scope.row)">修改</el-button>
        <el-button type="text"
                   @click="deleteHandle(scope.row)">删除</el-button>

      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible"
                   ref="addOrUpdate"
                   @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
  import { tableOption } from '@/crud/job/label'
  import AddOrUpdate from './label-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          id: null,
          name: null
        },
        dataList: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        dataListLoading: false,
        tableOption: tableOption,
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    created () {
    },
    mounted () {
    },
    methods: {
      // 获取数据列表
      getDataList (page, params) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/v1/job/label/list'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize
              }
            )
          )
        }).then(({ data }) => {
          console.log(data)
          this.dataList = data.data.records
          this.page.total = data.data.total
          this.dataListLoading = false
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row)
        })
      },
      // 删除
      deleteHandle (row) {
        this.$confirm('确定进行删除操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let param = {
            id: row.id
          }
          this.$http({
            url: this.$http.adornUrl('/v1/job/label/delete'),
            method: 'post',
            data: this.$http.adornData(param)
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        }).catch(() => { })
      },
      /**
       * 刷新回调
       */
      refreshChange () {
        this.getDataList(this.page)
      },
      // 条件查询
      searchChange (params, done) {
        done()
        this.getDataList(this.page, params)
      },
      // 多选变化
      selectionChange (val) {
        this.dataListSelections = val
      }
    }
  }
</script>
